<?php
if (! defined ( 'BASEPATH' ))
    exit ( 'No direct script access allowed' );

class Pasien extends Controller {
    public function __construct() {
        parent::__construct ();
        $this->_data = array ();
        $this->load->model ( 'pasien_model' );
        isController('klinik','pasien');
    }
    protected function _view($template, $data = array(), $result = false) {
        if ($data && is_array ( $data )) {
            return $this->load->view ( $template, $data, $result );
        }
        return $this->load->view ( $template, $this->_data, $result );
    }
    public function index() {
        redirect ( 'klinik/pasien/view' );
    }
    protected function _executeView() {
        $array = $this->uri->uri_to_assoc ( 3, array ('id', 'action' ) );
        // delete action
        if (isAccess('klinik','pasien','delete') && $array ['action'] == 'delete' && $array ['id']) {
            if ($this->pasien_model->delete($array['id'])) {
                $this->_data['errorMessage'] =  'Berhasil Delete Pasien' ;
                $this->_data['isSuccess'] =  true ;
            } else {
                $this->_data['errorMessage'] =  'Gagal Delete Pasien' ;
            }
        }
    }
    public function view() {
    	if (! isAccess ( 'klinik', 'pasien', 'view'))
            redirect (); 
        $array = $this->uri->uri_to_assoc ( 3, array ('id', 'name', 'action', 'page' ) );
        $this->_executeView ();
        $array['page'] = (int) $array['page'];

        //Load
        $this->load->helper('inflector');
        
        //Uri
        $name = $this->input->post('name') ? $this->input->post('name') : 'all';
        $array['name'] = $array['name'] ? $array['name'] : underscore($name);
        
        //Where
        $where = array();
        if( $array['name'] != 'all' )
        	$where['pasienNama'] = str_replace('_', ' ', $array['name']);
        
        $total = $this->pasien_model->getCount($where);
        if (($array['page'] + 1) > $total)
            $array['page'] = 0;
            
        $this->load->library('pagination');    
        $limit = $this->config->item('klinik_limit');
        $config = $this->config->item('paging');
        $config['base_url'] = site_url('klinik/pasien/view/name/'.$array['name'].'/page/');
        $config['uri_segment'] = 6;
        $config['total_rows'] = $total;
        $config['per_page'] = $limit;
        
        $this->pagination->initialize($config);
        $this->_data['paging'] =  $this->pagination->create_links();
        
        $dataView = $this->pasien_model->getList($limit, $array['page'], $where);
        $this->_data['dataView'] = $dataView;
        
        $this->_data['uri_to_assoc'] = $array;
        $this->_view ( 'klinik/pasienView' );
    }
    protected function _executeAdd() {
        $array = $this->uri->uri_to_assoc ( 3, array ('kode', 'action' ) );
        
        if ($this->input->post ( 'submit' ) == 'Simpan' && $this->input->post ( 'kirim' ) == 'kirim') {
            $pasienNama = trim ( $this->input->post ( 'pasienNama' ) );
            $pasienJenKel = trim ( $this->input->post ( 'pasienJenKel' ) );
            $pasienTglLahir = trim ( $this->input->post ( 'pasienTglLahir' ) );
            $pasienNoTelp = trim ( $this->input->post ( 'pasienNoTelp' ) );
            $pasienEmail = trim ( $this->input->post ( 'pasienEmail' ) );
            $pasienAlamat = trim ( $this->input->post ( 'pasienAlamat' ) );
            
            $data = array ();
            $data ['pasienNama'] = $pasienNama;
            $data ['pasienJenKel'] = $pasienJenKel;
            $data ['pasienTglLahir'] = $pasienTglLahir;
            $data ['pasienNoTelp'] = $pasienNoTelp;
            $data ['pasienEmail'] = $pasienEmail;
            $data ['pasienAlamat'] = $pasienAlamat;
            
            $errors = array ();
            if (empty ( $pasienNama ))
                $errors [] = 'Input Nama';
            if (empty ( $pasienTglLahir ))
                $errors [] = 'Input Tgl Lahir';
            if (empty ( $pasienNoTelp ))
                $errors [] = 'Input No Telp';
            if (empty ( $pasienAlamat ))
                $errors [] = 'Input Alamat';    
  
            if ($errors) {
                $this->_data['errorMessage'] =  implode ( '<br />', $errors ) ;
            } elseif ($this->pasien_model->add ( $data )) {
                $this->_data['errorMessage'] =  'Berhasil Tambah Pasien' ;
                $_POST = array();
                $this->_data['isSuccess'] =  true ;
            } else {
                $this->_data['errorMessage'] =  'Gagal Tambah Pasien' ;
            }
        
        }
    }

    public function add() {
    	if (! isAccess ( 'klinik', 'pasien', 'add'))
            redirect (); 
        $array = $this->uri->uri_to_assoc ( 3, array ('action' ) );

        $this->_data['listTipePasien'] = $this->config->item('listTipePasien');
        $this->_data['listJenKel'] = $this->config->item('listJenKel');
        $this->_data['listAgama'] = $this->config->item('listAgama');
        $this->_data['listKawin'] = $this->config->item('listKawin');
        $this->_data['listGolDarah'] = $this->config->item('listGolDarah');
          
        $this->_executeAdd ();
        $this->_view ( 'klinik/pasienAdd' );
    }

    protected function _executeEdit() {
        $array = $this->uri->uri_to_assoc ( 3, array ('id', 'action' ) );
        
        if ($this->input->post ( 'submit' ) == 'Simpan' && $this->input->post ( 'kirim' ) == 'kirim') {
        	
            $pasienTipe = trim ( $this->input->post ( 'pasienTipe' ) );
        	$pasienNama = trim ( $this->input->post ( 'pasienNama' ) );
            $pasienJenKel = trim ( $this->input->post ( 'pasienJenKel' ) );
            $pasienTmpLahir = trim ( $this->input->post ( 'pasienTmpLahir' ) );
            $pasienTglLahir = trim ( $this->input->post ( 'pasienTglLahir' ) );
            
            $pasienNoTelp = trim ( $this->input->post ( 'pasienNoTelp' ) );
            $pasienAlamat = trim ( $this->input->post ( 'pasienAlamat' ) );
            $pasienPekerjaan = trim ( $this->input->post ( 'pasienPekerjaan' ) );
            $pasienAgama = trim ( $this->input->post ( 'pasienAgama' ) );
            $pasienKawin = trim ( $this->input->post ( 'pasienKawin' ) );
            
			$pasienGolDarah = trim ( $this->input->post ( 'pasienGolDarah' ) );
			$pasienNamaPenanggung = trim ( $this->input->post ( 'pasienNamaPenanggung' ) );
			$pasienAlamatPenanggung = trim ( $this->input->post ( 'pasienAlamatPenanggung' ) );
			$pasienHubunganPenanggung = trim ( $this->input->post ( 'pasienHubunganPenanggung' ) );
            
            $data = array ();
            $data ['pasienTipe'] = $pasienTipe;
            $data ['pasienNama'] = $pasienNama;
            $data ['pasienJenKel'] = $pasienJenKel;
            $data ['pasienTmpLahir'] = $pasienTmpLahir;
            $data ['pasienTglLahir'] = $pasienTglLahir;
            
            $data ['pasienNoTelp'] = $pasienNoTelp;
            $data ['pasienAlamat'] = $pasienAlamat;
            $data ['pasienPekerjaan'] = $pasienPekerjaan;
            $data ['pasienAgama'] = $pasienAgama;
            $data ['pasienKawin'] = $pasienKawin;
            
            $data ['pasienGolDarah'] = $pasienGolDarah;
            $data ['pasienNamaPenanggung'] = $pasienNamaPenanggung;
            $data ['pasienAlamatPenanggung'] = $pasienAlamatPenanggung;
            $data ['pasienHubunganPenanggung'] = $pasienHubunganPenanggung;
            
            $errors = array ();
            if (empty ( $pasienTipe ))
                $errors [] = 'Input Tipe Pasien';    
            if (empty ( $pasienNama ))
                $errors [] = 'Input Nama';
            if (empty ( $pasienJenKel ))
                $errors [] = 'Input Jenis Kelamin';     
            if (empty ( $pasienTmpLahir ))
                $errors [] = 'Input Tempat Lahir';    
            if (empty ( $pasienTglLahir ))
                $errors [] = 'Input Tanggal Lahir';
                
            if (empty ( $pasienNoTelp ))
                $errors [] = 'Input No Telp';
            if (empty ( $pasienAlamat ))
                $errors [] = 'Input Alamat';
            if (empty ( $pasienPekerjaan ))
                $errors [] = 'Input Pekerjaan';
            if (empty ( $pasienAgama ))
                $errors [] = 'Input Agama';    
			if (empty ( $pasienKawin ))
                $errors [] = 'Input Kawin';
                
            if (empty ( $pasienGolDarah ))
                $errors [] = 'Input Gol Darah';
            if (empty ( $pasienNamaPenanggung ))
                $errors [] = 'Input Nama Penanggung';
            if (empty ( $pasienAlamatPenanggung ))
                $errors [] = 'Input Nama Penanggung';
            if (empty ( $pasienHubunganPenanggung ))
                $errors [] = 'Input Hubungan';
                
            if ($errors) {
                $this->_data['errorMessage'] =  implode ( '<br />', $errors ) ;
            } elseif ($this->pasien_model->update ( $array['id'], $data )) {
                $this->_data['errorMessage'] =  'Berhasil Edit Pasien' ;
                unset($_POST);
                $this->_data['isSuccess'] =  true ;
                
                $pasien = $this->pasien_model->get ( $array['id'] );
                $this->_data['dataEdit'] = $pasien;    
            } else {
                $this->_data['errorMessage'] =  'Gagal Edit Pasien' ;
            }
        }    
    }

    public function edit() {
    	if (! isAccess ( 'klinik', 'pasien', 'edit'))
            redirect (); 
        $array = $this->uri->uri_to_assoc ( 3, array ('id', 'from' ) );
        if ( !$array ['id'] || !$array ['from'] )
            show_404 ();
            
        $pasien = $this->pasien_model->get ( $array ['id'] );
        $this->_data['dataEdit'] = $pasien;    
        if (! $pasien)
            show_404 ();
            
        $this->_data['listTipePasien'] = $this->config->item('listTipePasien');
        $this->_data['listJenKel'] = $this->config->item('listJenKel');
        $this->_data['listAgama'] = $this->config->item('listAgama');
        $this->_data['listKawin'] = $this->config->item('listKawin');
        $this->_data['listGolDarah'] = $this->config->item('listGolDarah');
        
        $this->_executeEdit();
        $this->_data['uri'] = $array;
        $this->_view ( 'klinik/pasienEdit' );
    }
    
    //Rekam Medis
	protected function _executeRekamMedis() {
        $array = $this->uri->uri_to_assoc ( 3, array ('id', 'action' ) );
        // delete action
        if ($array ['action'] == 'delete' && $array ['id']) {
            if ($this->registrasi_model->delete($array['id'])) {
                $this->_data['errorMessage'] =  'Berhasil Delete Rekam Medis' ;
                $this->_data['isSuccess'] =  true ;
            } else {
                $this->_data['errorMessage'] =  'Gagal Delete Rekam Medis' ;
            }
        }
    }
    public function rekamMedis() {
    	if (! isAccess ( 'klinik', 'pasien', 'rekammedis'))
            redirect (); 
        $array = $this->uri->uri_to_assoc ( 3, array ('id', 'pasienId', 'from', 'action', 'page' ) );
        if( !$array['pasienId'] )
        	show_404();
        	
		//Load
        $this->load->model('registrasi_model');	
        	
        $this->_executeRekamMedis ();
        $array['page'] = (int) $array['page'];
        
        //Uri
        $array['pasienId'] = get_data($array,'pasienId') ? get_data($array,'pasienId') : 'all';
        
        //Where
        $where = array();
        $where['rmStatus'] = array( 'Selesai' );
        if( $array['pasienId'] != 'all' )
        	$where['pasienId'] = $array['pasienId'];
        
        $total = $this->registrasi_model->getCount($where);
        
        $this->load->library('pagination');
        if (($array['page'] + 1) > $total)
            $array['page'] = 0;
        $limit = $this->config->item('klinik_limit');
        $config = $this->config->item('paging');
       	$config['base_url'] = site_url('klinik/pasien/rekamMedis/pasienId/'.$array['pasienId'].'/page/');
        $config['uri_segment'] = 6;
        $config['total_rows'] = $total;
        $config['per_page'] = $limit;
        
        $this->pagination->initialize($config);
        $this->_data['paging'] =  $this->pagination->create_links();
        
        $dataView = $this->registrasi_model->getList($where, $limit, $array['page']);
        $this->_data['dataView'] = $dataView;
    
        //Data
       	$dataPasien = $this->pasien_model->get( $array['pasienId'] );
        $this->_data['dataPasien'] = $dataPasien;
        
        $this->_data['uri_to_assoc'] = $array;
        $this->_view ( 'klinik/pasienRekamMedis' );
    }
    
	public function detail() {
    	if (! isAccess ( 'klinik', 'pasien', 'rekammedis'))
            redirect (); 
        $array = $this->uri->uri_to_assoc ( 3, array ('id', 'pasienId', 'from', 'action', 'page' ) );
        if( !$array['pasienId'] )
        	show_404();
 
        //Data
       	$dataPasien = $this->pasien_model->get( $array['pasienId'] );
        $this->_data['dataPasien'] = $dataPasien;
        
        $this->_data['uri_to_assoc'] = $array;
        $this->_view ( 'klinik/pasienDetail' );
    }
    
	//Json 
	public function getJson() {
		$field = trim ( $this->input->post('field') ) ;
		$value = trim ( $this->input->post('value') ) ;
		
		$where = array();
		$where['field'] = $field;
		$where['value'] = $value;
    	$result = $this->pasien_model->getJson( $where );

    	echo json_encode( $result );
    }

   public function preview() {
        $array = $this->uri->uri_to_assoc ( 3, array ('id', 'pasienId') );
 		if ( !$array ['pasienId'] || !$array ['id'] )
            show_404 ();
            
        //Load
        $this->load->model( array('alat_model', 'pemeriksaan_model', 'registrasi_model') );
            
        //Data General
        $dataPasien = $this->registrasi_model->get ( $array ['id'] );
        $this->_data['dataPasien'] = $dataPasien;    
        if (! $dataPasien)
            show_404 ();
            
        //Data Alat
        $dataAlat = $this->alat_model->getListRM( array('rmId'=>$array['id']) );    
        $this->_data['dataAlat'] = $dataAlat;
        
        //Data Alat
        $dataPemeriksaan = $this->pemeriksaan_model->getListRM( array('rmId'=>$array['id']) );    
        $this->_data['dataPemeriksaan'] = $dataPemeriksaan;
        
        $this->_data['uri'] = $array;
        $this->_view ( 'klinik/pasienPreview' );
    }
}
